//
//  ActiveDetailViewController.m
//  Cycling
//
//  Created by 迟国强 on 2023/4/10.
//

#import "ActiveDetailViewController.h"
#import "ActiveMessageCell.h"
#import "HeightTableCell.h"
#import "AllMessageTableViewCell.h"
#import "RouteShareTableCell.h"
#import "MapRequest.h"

#import "RouteRequest.h"
#import "CyclingRouteDetailModel.h"
#import "CyclingRouteEditModel.h"

#import "EditRouteController.h"

#import "Cycling-Swift.h"

@interface ActiveDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;  // 列表数据
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) CyclingRouteEditModel *editModel;
@property (nonatomic, strong) CyclingRouteHeartModel *heartModel;

@property (nonatomic) BOOL dataHasLoad;

@end

@implementation ActiveDetailViewController

- (void)viewDidLoad {
    
    self.dataHasLoad = NO;
    
    [super viewDidLoad];
    self.title = CycLocalizedString(@"Details", @"运动详情");
    self.view.backgroundColor = rgbHex(0xF8F8F8, 1);
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(@0);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 202)];
    imageView.backgroundColor = UIColor.clearColor;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    self.tableView.tableHeaderView = imageView;
    self.imageView = imageView;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 94)];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(30, 10, SCREEN_W - 60, 54);
    saveBtn.backgroundColor = rgb(30, 214, 194);
    [saveBtn setTitle:CycLocalizedString(@"Add as private route", @"添加私人路线") forState:UIControlStateNormal];
    [saveBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    saveBtn.layer.cornerRadius = 27;
    saveBtn.layer.masksToBounds = YES;
    [saveBtn addTarget:self action:@selector(saveRouteAction) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:saveBtn];
    self.tableView.tableFooterView = footer;
    
    self.tableView.tableFooterView.hidden = !self.dataHasLoad;
    
    self.editModel = [[CyclingRouteEditModel alloc] init];
    self.editModel.cid = self.cid;
    
    [self loadData];
}

- (void)loadData {
    
    [self showLoadingViewWithText:@""];
    [RouteRequest getCyclingRouteDetailWithId:self.cid result:^(CyclingRouteDetailModel *model) {
        self.editModel.detailModel = model;
        if(model.fulllineimg) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.fulllineimg]];
        } else {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.lineimg]];
        }
        
        [self removeLoadingView];
        self.dataHasLoad = YES;
        [self.tableView reloadData];
        self.tableView.tableFooterView.hidden = !self.dataHasLoad;
        NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:model.start];
        NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:model.end];
        [self requestHeartDatasFrom:fromDate toDate:endDate complete:^(BOOL success, CyclingRouteHeartModel * heartModel) {
            self.heartModel = heartModel;
            dispatch_async(dispatch_get_main_queue(), ^{
                if(heartModel) {
                    [self.tableView reloadData];
                }
            });
        }];
    }];
}

- (void)saveRouteAction
{
    EditRouteController *editRouteVC = [[EditRouteController alloc] init];
    editRouteVC.editModel = self.editModel;
    editRouteVC.isAdd = YES;
    [self.navigationController pushViewController:editRouteVC animated:YES];
}

#pragma mark - tableView delegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!self.dataHasLoad) {
        return 0;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ActiveMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActiveMessageCell"];
        cell.model = self.editModel.detailModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.row == 1) {
        HeightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeightTableCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.heightModel = self.editModel.detailModel.highly;
        return cell;
    } else if (indexPath.row == 2) {
        HeightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeightTableCell"];
        cell.speedModel = self.editModel.detailModel.speed;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.row == 3) {
        HeightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeightTableCell"];
        if(self.heartModel) {
            cell.heartModel = self.heartModel;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.clipsToBounds = YES;
        return cell;
    } else if (indexPath.row == 4) {
        AllMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllMessageTableViewCell"];
        cell.detailModel = self.editModel.detailModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        RouteShareTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RouteShareTableCell"];
        cell.routeSaved = self.editModel.routeSave;
        cell.facebookShared = self.editModel.facebookShared;
        cell.valueChangedBlock = ^(NSInteger type, BOOL value) {
            if(type == 0) {
                self.editModel.facebookShared = value;
            } else if(type == 1) {
                self.editModel.routeSave = value;
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return UITableViewAutomaticDimension;
    } else if (indexPath.row == 1) {
        return 225;
    } else if (indexPath.row == 2) {
        return 225;
    } else if (indexPath.row == 3) {
        if(!self.heartModel) {
            return 0;
        }
        return 225;
    } else if (indexPath.row == 4) {
        return UITableViewAutomaticDimension;
    } else {
        return 271;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = rgbHex(0xF8F8F8, 1);
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
        if (@available(iOS 11.0, *) ) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"ActiveMessageCell" bundle:nil] forCellReuseIdentifier:@"ActiveMessageCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"HeightTableCell" bundle:nil] forCellReuseIdentifier:@"HeightTableCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"RouteTypeTableCell" bundle:nil] forCellReuseIdentifier:@"RouteTypeTableCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"RouteImageTableCell" bundle:nil] forCellReuseIdentifier:@"RouteImageTableCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"RouteShareTableCell" bundle:nil] forCellReuseIdentifier:@"RouteShareTableCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"AllMessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"AllMessageTableViewCell"];

    }
    return _tableView;
}

@end
